package com.huadi.firematerialident.controller;

import com.huadi.firematerialident.dao.UserDaoImpl;
import com.huadi.firematerialident.po.*;
import com.huadi.firematerialident.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.net.PasswordAuthentication;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    UserDaoImpl userDao;
    @Autowired
    NormalUserServiceImpl normalUserService;
    @Autowired
    IdentifierServiceImpl identifierService;
    @Autowired
    AdminServiceImpl adminService;
    @Autowired
    ReviewerServiceImpl reviewerService;


    @RequestMapping("/tologin")
    public String userlogin(){
        return "/user/login";
    }


    @RequestMapping("/login")
    public String login(@RequestParam("username") String username,
                       @RequestParam("password") String password,
                        Model model){
        //System.out.println(username);
        int code = userService.login(username,password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        switch (code){
            case 0: {
                System.out.println("登录失败");
                model.addAttribute("msg", "登录失败!");
                return "/user/login";
            }
            case 1:{
                user.setRole("normaluser");
                userService.addUser(user);
                model.addAttribute("msg","登录成功!");
                return "/app/appcommit";
            }
            case 2:{
                user.setRole("identifier");
                userService.addUser(user);
                model.addAttribute("msg","鉴定员登录成功!");
                return "/app/appcommit";
            }
            case 3:{
                user.setRole("reviewer");
                userService.addUser(user);
                model.addAttribute("msg","审查员登录成功!");
                return "/app/appcommit";
            }
            case 4:{
                user.setRole("admin");
                userService.addUser(user);
                model.addAttribute("msg","管理员登录成功!");
                return "/app/appcommit";
            }
        }
        model.addAttribute("msg","登录异常");
        return "/user/login";
    }



    @RequestMapping("/register")
    public String register(@RequestParam("username") String username,
                           @RequestParam("firstPassword") String fPassword,
                           @RequestParam("secondPassword") String sPassword,
                           Model model){
        System.out.println(username);
        System.out.println(fPassword);
        System.out.println(sPassword);
        int code=userService.register(username,fPassword,sPassword);
        switch (code){
            case 0:{
                model.addAttribute("msg","两次输入密码不一致！请重新输入");
                return "/user/register";
            }
            case 1:{
                model.addAttribute("msg","用户名已被使用！请重新输入");
                return "/user/register";
            }
            case 2:{
                model.addAttribute("msg","注册成功！请登录！");
                return "/user/login";
            }
            case 3:{
                model.addAttribute("msg","请将所有信息填写完整！");
                return "/user/register";
            }
        }
        return "/user/login";
    }

    @RequestMapping("/logout")
    public String logout(Model model){
        userService.logout();
        model.addAttribute("msg","已退出登录！");
        return "user/login";
    }

    @RequestMapping("/userCenter")
    public String UpdateUserInfo(@RequestParam("Name") String Name,
                                 @RequestParam("Sex") String Sex,
                                 @RequestParam("Birth") String Birth,
                                 @RequestParam("Phone") String Phone,
                                 Model model){
        String role = userService.getRole();
        String ID;
        String Count;
        String Password;
        boolean LogicalAddDel;
        switch (role){
            case "normaluser":
                ID = normalUserService.getNUserID();
                Count = (String) normalUserService.selectNormalUserByID(ID).get(0).get("NUserCount");
                Password = (String) normalUserService.selectNormalUserByID(ID).get(0).get("NPassword");
                LogicalAddDel = (boolean) normalUserService.selectNormalUserByID(ID).get(0).get("LogicalAddDel");
                NormalUser normalUser = new NormalUser();
                normalUser.setNUserID(ID);
                normalUser.setNUserCount(Count);
                normalUser.setNPassword(Password);
                normalUser.setNName(Name);
                normalUser.setNSex(Sex);
                LocalDate btime1 = LocalDate.parse(Birth);
                normalUser.setNBirth(btime1);
                normalUser.setNPhone(Phone);
                normalUser.setLogicalAddDel(LogicalAddDel);
                if(normalUserService.updateNormalUser(normalUser)!=0)
                    model.addAttribute("msg","修改成功");
                else
                    model.addAttribute("msg","修改失败");

                break;
            case "admin":
                ID = adminService.getAdminID();
                Count = (String) adminService.selectAdminByID(ID).get(0).get("AdminCount");
                Password = (String) adminService.selectAdminByID(ID).get(0).get("AdminPassword");
                LogicalAddDel = (boolean) adminService.selectAdminByID(ID).get(0).get("LogicalAddDel") ;
                Admin admin = new Admin();
                admin.setAdminID(ID);
                admin.setAdminCount(Count);
                admin.setAdminPassword(Password);
                admin.setAdminName(Name);
                admin.setAdminSex(Sex);
                LocalDate btime2 = LocalDate.parse(Birth);
                admin.setAdminBirth(btime2);
                admin.setAdminPhone(Phone);
                admin.setLogicalAddDel(LogicalAddDel);

                if(adminService.updateAdmin(admin)!=0)
                    model.addAttribute("msg","修改成功");
                else
                    model.addAttribute("msg","修改失败");
                break;
            case "identifier":
                ID = identifierService.getIdentID();
                Count = (String) identifierService.selectIdentifierByID(ID).get(0).get("IdentCount");
                Password = (String) identifierService.selectIdentifierByID(ID).get(0).get("IdentPassword");
                LogicalAddDel = (boolean) identifierService.selectIdentifierByID(ID).get(0).get("LogicalAddDel");
                Identifier ident = new Identifier();
                ident.setIdentID(ID);
                ident.setIdentCount(Count);
                ident.setIdentPassword(Password);
                ident.setIdentName(Name);
                ident.setIdentSex(Sex);
                LocalDate btime3 = LocalDate.parse(Birth);
                ident.setIdentBirth(btime3);
                ident.setIdentPhone(Phone);
                ident.setLogicalAddDel(LogicalAddDel);

                if( identifierService.updateIdentifier(ident)!=0)
                    model.addAttribute("msg","修改成功");
                else
                    model.addAttribute("msg","修改失败");
                break;
            case "reviewer":
                ID = reviewerService.getRevID();                
                Count = (String) reviewerService.selectReviewerByID(ID).get(0).get("RevCount");
                Password = (String) reviewerService.selectReviewerByID(ID).get(0).get("RevPassword");
                LogicalAddDel = (boolean) reviewerService.selectReviewerByID(ID).get(0).get("LogicalAddDel");
                Reviewer rev = new Reviewer();
                rev.setRevID(ID);
                rev.setRevCount(Count);
                rev.setRevPassword(Password);
                rev.setRevName(Name);
                rev.setRevSex(Sex);
                LocalDate btime4 = LocalDate.parse(Birth);
                rev.setRevBirth(btime4);
                rev.setRevPhone(Phone);
                rev.setLogicalAddDel(LogicalAddDel);

                if( reviewerService.updateReviewer(rev)!=0)
                    model.addAttribute("msg","修改成功");
                else
                    model.addAttribute("msg","修改失败");
                break;
        }

        return "/user/usercenter";
    }

    @RequestMapping("/userInfo")
    @ResponseBody
    public String UserInfo(Model model){
        String role = userService.getRole();
        String ID;

        List<Map<String,Object>> list1 = new LinkedList<>();
        List<Map<String,Object>> list2 = new LinkedList<>();
        Map<String,Object> map = new LinkedHashMap<>();
        switch (role){
            case "normaluser":
                ID = normalUserService.getNUserID();
                list1 = normalUserService.selectNormalUserByID(ID);
                map.put("ID",list1.get(0).get("NuserID"));
                map.put("Count",list1.get(0).get("NUserCount"));
                map.put("Name",list1.get(0).get("NName"));
                map.put("Sex",list1.get(0).get("NSex"));
                map.put("Birth",list1.get(0).get("NBirth"));
                map.put("Phone",list1.get(0).get("NPhone"));
                break;
            case "admin":
                ID = adminService.getAdminID();
                list1 = adminService.selectAdminByID(ID);
                map.put("ID",list1.get(0).get("AdminID"));
                map.put("Count",list1.get(0).get("AdminCount"));
                map.put("Name",list1.get(0).get("AdminName"));
                map.put("Sex",list1.get(0).get("AdminSex"));
                map.put("Birth",list1.get(0).get("AdminBirth"));
                map.put("Phone",list1.get(0).get("AdminPhone"));
                break;
            case "identifier":
                ID = identifierService.getIdentID();
                list1 = identifierService.selectIdentifierByID(ID);
                map.put("ID",list1.get(0).get("IdentID"));
                map.put("Count",list1.get(0).get("IdentCount"));
                map.put("Name",list1.get(0).get("IdentName"));
                map.put("Sex",list1.get(0).get("IdentSex"));
                map.put("Birth",list1.get(0).get("IdentBirth"));
                map.put("Phone",list1.get(0).get("IdentPhone"));
                break;
            case "reviewer":
                ID = reviewerService.getRevID();
                list1 = reviewerService.selectReviewerByID(ID);
                map.put("ID",list1.get(0).get("RevID"));
                map.put("Count",list1.get(0).get("RevCount"));
                map.put("Name",list1.get(0).get("RevName"));
                map.put("Sex",list1.get(0).get("RevSex"));
                map.put("Birth",list1.get(0).get("RevBirth"));
                map.put("Phone",list1.get(0).get("RevPhone"));
                break;
        }
        list2.add(map);
        System.out.println("list1:"+list1);
        System.out.println("list2:"+list2);
        return userService.ListToJSON(list2.size(),list2);
    }
}
